package org.iteam.xlz.manager.action;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.shiro.SecurityUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.core.Constant;
import org.iteam.core.model.sys.SysUser;
import org.iteam.core.service.CenterService;
import org.iteam.core.service.sys.SysUserService;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.JsonAction;

/**
 * 管理中心控制器
 */
@Results({ @Result(name = "CENTER", type = "freemarker", location = "center.html") })
@SuppressWarnings("unchecked")
public class CenterAction extends JsonAction {

	/**
	 * 管理中心首页
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/center", interceptorRefs = @InterceptorRef("defaultStack"))
	public String center() throws Exception {
		String account = ShiroUtil.currentAccount();
		SysUser user = userService.show(account);
		user.setPassword(null);
		SecurityUtils.getSubject().getSession().setAttribute(Constant.SESSION_CURRENT_USER, user);
		map.put("roles", userService.getRolesByUser(account));
		return "CENTER";
	}

	/**
	 * 异步加载菜单(post)
	 * 
	 * @return
	 */
	@Action(value = "/loadMenu/{id}", interceptorRefs = @InterceptorRef("defaultStack"))
	public String loadMenu() {
		try {
			array = sevice.getMenusByRole(id);
			SecurityUtils.getSubject().getSession().setAttribute(Constant.SESSION_CURRENT_ROLE_CODE, id);
		} catch (SQLException e) {
			array = new ArrayList<Object>();
		}
		return ARRAY;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 4256463424516331103L;

	private String id;
	private CenterService sevice = CenterService.getInstance();
	private SysUserService userService = SysUserService.getInstance();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}